package com.mail.server.controller;


import com.mail.model.db.PmsProductCategory;
import com.mail.model.dto.ProductCategoryDto;
import com.mail.model.vo.ChildrenCategoryVo;
import com.mail.model.vo.PageResult;
import com.mail.server.common.R;
import com.mail.server.service.PmsProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 产品分类 前端控制器
 * </p>
 *
 * @author WJZ
 * @since 2022-06-20
 */
@RestController
@RequestMapping("/productCategory")
public class PmsProductCategoryController {

    @Autowired
    private PmsProductCategoryService productCategoryService;

    /**
     * 添加商品分类
     *
     * @param productCategoryDto
     * @return
     */
    @PostMapping("/create")
    public R createCategory(@RequestBody ProductCategoryDto productCategoryDto) {
        productCategoryService.createCategory(productCategoryDto);
        return R.success(null);
    }

    /**
     * 删除商品分类
     *
     * @param categoryId
     * @return
     */
    @PostMapping("/delete/{id}")
    public R deleteCategory(@PathVariable(value = "id") Integer categoryId) {
        productCategoryService.deleteCategory(categoryId);
        return R.success(null);
    }

    /**
     * 分页查询商品分类
     *
     * @param parentId
     * @return
     */
    @GetMapping("/list/{parentId}")
    public R queryCategoryList(@PathVariable Integer parentId, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult pageResult = productCategoryService.queryCategoryList(parentId, pageNum, pageSize);
        return R.success(pageResult);
    }

    /**
     * 查询所有一级分类及子分类
     *
     * @return
     */
    @GetMapping("/list/withChildren")
    public R queryWithChildrenCategory() {
        List<ChildrenCategoryVo> childrenCategoryVos = productCategoryService.queryWithChildrenCategory();
        return R.success(childrenCategoryVos);
    }

    /**
     * 修改导航栏显示状态
     */
    @PostMapping("/update/navStatus")
    public R changeNavStatus(@RequestParam List<Integer> ids,Integer navStatus) {
        productCategoryService.changeNavStatus(ids, navStatus);
        return R.success(null);
    }

    /**
     * 修改显示状态
     */
    @PostMapping("/update/showStatus")
    public R changeShowStatus(@RequestParam List<Integer> ids,Integer showStatus) {
        productCategoryService.changeShowStatus(ids,showStatus);
        return R.success(null);
    }

    /**
     * 修改商品分类
     * @param id
     * @param productCategoryDto
     * @return
     */
    @PostMapping("/update/{id}")
    public R changeCategory(@PathVariable Integer id,@RequestBody ProductCategoryDto productCategoryDto){
        productCategoryService.changeCategory(id,productCategoryDto);
        return R.success(null);
    }

    /**
     * 根据id获取商品分类
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R queryCategoryById(@PathVariable Integer id){
        PmsProductCategory pmsProductCategory = productCategoryService.queryCategoryById(id);
        return R.success(pmsProductCategory);
    }
}
